#include "TextOperator.h"


TextOperator::TextOperator(const QString &fileName, int interval):
    m_fileName(fileName)
{
    m_interval = interval;
}

void TextOperator::readData()
{
//    qDebug() << "Thread ID: " << QThread::currentThreadId();
    //1.打开文件
    QFile file(m_fileName);
    if( !file.open(QIODevice::ReadOnly) ) {
        qDebug() << "Open file " + m_fileName +
                    " error!!";
        return ;
    }

    QString string;

    string = file.readAll();

    double length = string.length();
    //qDebug()<<string;
    qDebug()<<length;
    QStringList strList = string.split(",",QString::SkipEmptyParts);
    if(length == 0)
    {
        qDebug()<<"测试异常";
    }
    else if(length >= 87 && length <= 97)
    {
        //小车编号,网络,x（4位小数）,y（4位小数） ,角度（4位小数）,速度（4位小数） ,角速度（4位小数）,电量（9-12）int ,状态（1/2/0） ,负载（0/1),
        m_data.num     = strList[3].toDouble();
        m_data.net     = strList[4].toDouble();
        m_data.x       = strList[5].toDouble()*10;
        m_data.y       = strList[6].toDouble()*10;
        m_data.theta   = strList[7].toDouble();
        m_data.ele     = strList[10].toDouble();
        m_data.p       = static_cast<RobotRunningState>(strList[11].toInt());
        m_data.load    = strList[12].toDouble();

        emit dataRefresh(m_data);
    }
    file.close();
}

void TextOperator::run()
{
    while (m_isRunning) {
//        qDebug() << "Run QThred ID: " << QThread::currentThreadId();
        readData();
        msleep(m_interval);
    }
}
